package com.sistemagestion.ventas.view;

import java.util.List;

import com.sistemagestion.R;
import com.sistemagestion.BO.FacturaBO;
import com.sistemagestion.Entities.Factura;
import com.sistemagestion.Utils.Constantes;
import com.sistemagestion.Utils.DialogConfirmacion;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.TextView;

public class VentaListado extends FragmentActivity{

	List<Factura> _facturas;
	FacturaBO facturaBO = new FacturaBO();
	VentaAdapter adapter;
	ListView lstVentas;
	TextView lblTotal;
	TextView lblTotalVentas;
	int total = 0;
	int totalventas = 0;
	int selectedpos;
	Factura _factura;
	private static final int ACTIVITY_MOSTRAR_VENTA = 0;
	
	@Override
	protected void onCreate(Bundle arg0) {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(arg0);
		setContentView(R.layout.lyt_listado_ventas);
		InitVars();
		registerForContextMenu(lstVentas);
	}
	
	
	private void InitVars(){
		_facturas = facturaBO.getAll();
		adapter = new VentaAdapter(this, R.layout.lyt_listado_ventas_item, _facturas);
		lstVentas = (ListView)findViewById(R.id.lstVentas);
		lstVentas.setAdapter(adapter);
		lblTotal = (TextView)findViewById(R.id.lblTotal);
		lblTotalVentas = (TextView)findViewById(R.id.lblTotalVentas);
		
		_facturas = facturaBO.getAll();
		
		for (Factura item : _facturas) {
			total += item.getTotal();
			totalventas += 1;
		}
		
		lblTotal.setText("Total: " + (Integer.toString(total)));
		lblTotalVentas.setText("Total de Ventas: " + (Integer.toString(totalventas)));
		
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		menu.setHeaderTitle("Opciones");
		inflater.inflate(R.menu.ctx_mn_lstventa, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		selectedpos = info.position;
		_factura = adapter.getItem(selectedpos);
		switch (item.getItemId()) {
		case R.id.itmDetalle:
			Intent intent = new Intent(this, VentaDetalle.class);
			intent.putExtra("OPERATION", Constantes.Operations.EDIT);
			intent.putExtra("FACTURA", _factura);
			startActivity(intent);
//			startActivityForResult(intent, ACTIVITY_MOSTRAR_VENTA);
			return true;
		}
		return super.onContextItemSelected(item);
	}
	
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
//		if(requestCode == ACTIVITY_MOSTRAR_VENTA && resultCode == this.RESULT_OK){
//			//Hacer algo, pero creo que no hace falta xD
//		}
//	}
}
